package com.nlp.visualization.controller.spring.controller;

import com.nlp.visualization.common.CONSTANTS;
import com.nlp.visualization.core.seg.SegmentType;
import com.nlp.visualization.core.seg.filter.SegFilter;
import com.nlp.visualization.pojo.common.AjaxReturn;
import com.nlp.visualization.pojo.lda.LDATheme;
import com.nlp.visualization.service.backEnd.IThemeManageService;
import com.nlp.visualization.service.impl.backend.ThemeManageServiceImpl;
import com.nlp.visualization.utils.PropertyUtil;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author LXM
 * @Title: NLPVisualizationTools
 * @Description:
 * @date 2018/5/15上午11:12
 */
@Controller
@RequestMapping("/crawler")
public class CrawlerController {

    @Autowired
    private IThemeManageService themeManageService;

    Logger logger = LoggerFactory.getLogger(CrawlerController.class);

    //LDA模型的路径
    private static final String LDA_MODEL_PATH = PropertyUtil.getInstance("config").getProperty("lda.base") +
            PropertyUtil.getInstance("config").getProperty("lda.news.origin");

    private String originNewsPath = PropertyUtil.getInstance("config").getProperty("lda.base")
            + PropertyUtil.getInstance("config").getProperty("lda.news.origin");

    private String segNewsPath = PropertyUtil.getInstance("config").getProperty("lda.base")
            + PropertyUtil.getInstance("config").getProperty("lda.news.segment");


    /**
     * 执行新闻的爬取
     *
     * @param map
     * @return
     */
    @RequestMapping("/news")
    @ResponseBody
    public AjaxReturn clawSinaNews(@RequestBody Map map) {

        Map res = new HashedMap();
        List l = new ArrayList();

        String start = (String) map.get("start");
        String end = (String) map.get("end");
        List<String> list = new ArrayList<>();
        list.add("sina");
        try {
            themeManageService.updateNewsSpider(start, end, list);

        } catch (Exception e) {
            logger.error("爬取异常", e);
            return new AjaxReturn(999, "爬取新浪新闻异常");
        }
        try {
            File file = new File(LDA_MODEL_PATH + "/" + start + ".json");
            String startFile = FileUtils.readFileToString(file);
            res.put("filename", start + ".json");
            res.put("content", startFile);
            l.add(res);
            return new AjaxReturn(CONSTANTS.ERROR_SUCCESS, "成功", l);

        } catch (IOException e) {
            logger.error("读取文件异常", e);
            return new AjaxReturn(888, "读取文件异常");
        }

    }


    @RequestMapping("/news/seg")
    @ResponseBody
    public AjaxReturn doSegNews(@RequestBody Map map) {
        ThemeManageServiceImpl service = new ThemeManageServiceImpl();
        String fileName = (String) map.get("fileName");
        SegFilter.FilterBuilder builder = new SegFilter.FilterBuilder();
        SegFilter filter = builder.enableStopwords(true).enablePunctuation(true).build();
        File newsJsonDir = new File(service.getOriginNewsPath());
        File newsSegTextDir = new File(service.getSegNewsPath());
        try {
            service.executeNewsSeg(newsJsonDir, newsSegTextDir, SegmentType.HANLP_CRF, filter);
            return new AjaxReturn(CONSTANTS.ERROR_SUCCESS, "成功");
        } catch (Exception e) {
            logger.error("分词异常", e);
            return new AjaxReturn(999, "分词异常");
        }

    }


    @RequestMapping("/news/train")
    @ResponseBody
    public AjaxReturn doTrainNews(@RequestBody Map map) {
        Map resMap = new HashedMap();

        try {
            themeManageService.trainAndSaveModel(15);
            String path = PropertyUtil.getInstance("config").getProperty("lda.base") +
                    PropertyUtil.getInstance("config").getProperty("lda.model") + "/" + ((String) map.get("fieldname")).replace("json", "model.txt");
            File file = new File(path);
            String fileStr = FileUtils.readFileToString(file);
            resMap.put("themes", fileStr);
            return new AjaxReturn(CONSTANTS.ERROR_SUCCESS, "成功", resMap);

        } catch (Exception e) {
            logger.error("训练完成", e);
            return new AjaxReturn(999, "训练异常");
        }

    }


    @RequestMapping("/news/models")
    @ResponseBody
    public AjaxReturn doGetmodels() {

        List list = new ArrayList();
        Map resMap = new HashedMap();
        //1、首先取得模型目录下的所有文件
        String pathDir = PropertyUtil.getInstance("config").getProperty("lda.base") +
                PropertyUtil.getInstance("config").getProperty("lda.model");

        File dir = new File(pathDir);
        File[] files = dir.listFiles();


        for (File f : files) {
            try {
                if (f.getName().indexOf("model.txt") > 0) {
                    Map map = new HashedMap();
                    map.put("topics", FileUtils.readFileToString(f));
                    map.put("topicNames", FileUtils.readFileToString(new File(dir + "/" + f.getName().replace("model.txt", "seq"))));
                    map.put("name", f.getName().replace(".model.txt", ""));
                    list.add(map);

                } else if (f.getName().indexOf(".seq") > 0) {
                }
            } catch (Exception e) {
                logger.error("读取文件异常", e);
                continue;
            }
        }

        return new AjaxReturn(CONSTANTS.ERROR_SUCCESS, "成功", list);
    }


}
